package cn.zjutleo.village_server.controller;

import cn.zjutleo.village_server.annotation.PassAuthentication;
import cn.zjutleo.village_server.annotation.RequiresLogin;
import cn.zjutleo.village_server.entity.CPSigned;
import cn.zjutleo.village_server.entity.CPSignedKey;
import cn.zjutleo.village_server.service.CPsignService;
import cn.zjutleo.village_server.service.CooperateService;
import cn.zjutleo.village_server.vo.ResponseBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author ：杜雨茜
 * @Description ：村民互助活动报名
 * @Date ：2021/12/23 15:32
 **/

@Api(tags = "村民互助活动报名模块")
@RestController
@RequestMapping("/api/cpsign")
@RequiresLogin(requiresRoles={"USER","qdhiw"})
public class CPSignController {
    @Autowired
    CPsignService cPsignService;
    @Autowired
    CooperateService cooperateService;
    /**
     * @author: 杜雨茜
     * @description: 互助活动报名
     * @param cpSignedKey
     * @return
     **/
    @RequiresLogin
    @ApiOperation("活动报名")
    @PostMapping("/sign")
    public ResponseBean<String> sign(@RequestBody CPSignedKey cpSignedKey){
        //先判断是否允许报名
        //获取当前时间
        Date now = new Date();
        //获取活动报名开始时间
        Date start = cooperateService.getsignStartDateTime(cpSignedKey.getCooperateid());
        Date end = cooperateService.getsignEndDateTime(cpSignedKey.getCooperateid());
        //在时间内
        if(now.compareTo(start)>0 && now.compareTo(end)<0)
        {
            CPSigned cpSigned = new CPSigned();
            cpSigned.setCooperateid(cpSignedKey.getCooperateid());
            cpSigned.setParticipantid(cpSignedKey.getParticipantid());
            cpSigned.setSignflag(1);
            //先搜索是否有报名记录
            //数据库没有报名记录
            if(cPsignService.getExistSign(cpSignedKey) == null)
            {
                //插入报名记录
                int i = cPsignService.addsign(cpSigned);
                if(i>0)
                    return new ResponseBean<>("success");
                else
                    return new ResponseBean<>("fail");
            }
            //数据库有记录，说明已经报过名了，不需要重复报名
            else
            {
                return new ResponseBean<>("signed");
            }
        }
        //在时间外
        else{
            return new ResponseBean<>("beyond sign time");
        }


    }

    /**
     * @author: 杜雨茜
     * @description: 取消报名
     * @param cpSignedKey
     * @return
     **/
    @ApiOperation("取消报名")
    @PostMapping("/unsign")
    public ResponseBean<String> unsign(@RequestBody CPSignedKey cpSignedKey){
        //先判断是否允许取消报名
        //获取当前时间
        Date now = new Date();
        //获取活动报名开始时间
        Date start = cooperateService.getsignStartDateTime(cpSignedKey.getCooperateid());
        Date end = cooperateService.getsignEndDateTime(cpSignedKey.getCooperateid());
        //在时间内
        if(now.compareTo(start)>0 && now.compareTo(end)<0) {
            //先搜索是否有报名记录
            //数据库没有报名记录
            if(cPsignService.getExistSign(cpSignedKey) == null)
            {
                return new ResponseBean<>("nosign");
            }
            else{
                if(cPsignService.deleteSign(cpSignedKey)>0){
                    return new ResponseBean<>("success");
                }else
                    return new ResponseBean<>("fail");
            }
        }
        //在时间外
        else{
            return new ResponseBean<>("beyond unsign time");
        }

    }
}
